package com.cardbaobao.cardbabyclient.ui.fragment.transactionrecord;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.callback.ItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.TransactionRecordData;
import com.cardbaobao.cardbabyclient.model.result.TransactionRecordDataResult;
import com.cardbaobao.cardbabyclient.ui.activity.TransactionDetailActivity;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.adapter.DebitRecordAdapter;
import com.cardbaobao.cardbabyclient.ui.fragment.base.BaseFragment;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.divider.VerticalItemDecoration;
import com.cardbaobao.cardbabyclient.widget.refresh.FooterLoadMore;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/13 11:19.
 */

public class DebitRecordFragment extends BaseFragment {
    private String prePageId;
    public void setBuriedInfo(String prePageId){
        this.prePageId=prePageId;
    }

    private static final int PAGE_START=1;
    private static final int TYPE=TransactionRecordData.TYPE_DEBIT_RECORD;//收款记录
    @BindView(R.id.srLayout)
    SmartRefreshLayout srLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.footerLoadMore)
    FooterLoadMore footerLoadMore;

    private List<TransactionRecordData.ShowerBean> data;
    private DebitRecordAdapter adapter;
    private int page=PAGE_START;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_debit_record;
    }

    @Override
    protected void initViewFinish(View view, Bundle savedInstanceState) {

        mCurrentPageId= Self.Page.app_debit_transaction_record_page;

        this.data=new ArrayList<>();
        footerLoadMore.setStatePreLoadingStr("上拉加载更多");
        srLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshLayout) {
                page++;
                loadData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                data.clear();
                adapter.notifyDataSetChanged();
                page=PAGE_START;
                loadData();
            }
        });

        LinearLayoutManager layoutManager=
                new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        VerticalItemDecoration decoration=new VerticalItemDecoration(Constant.DIVIDER_COLOR,1);
        decoration.setPaddingLeftRight(DensityUtil.dp2px(20),0);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(decoration);

        adapter=new DebitRecordAdapter(mContext,data);
        adapter.setItemClickedCallback(new ItemClickedCallback<TransactionRecordData.ShowerBean>() {
            @Override
            public void onItemClicked(TransactionRecordData.ShowerBean showerBean) {
                SelfUtils.buriedClickData(Self.Action.app_click_debit_record_item,
                        Self.Action.app_click_debit_record_item_des,
                        mEnterPageTime,
                        mCurrentPageId,prePageId);
                Intent intent=new Intent(mContext, TransactionDetailActivity.class);
                intent.putExtra(TransactionDetailActivity.ORDER_NUMBER_S,
                        showerBean.getOrderNumber());
                intent.putExtra(TransactionDetailActivity.DETAIL_TYPE_I,
                        TransactionDetailActivity.TYPE_DEBIT);
                intent.putExtra(BaseActivity.PRE_PAGE_ID_S,mCurrentPageId);
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);

        srLayout.autoRefresh();
    }

    private void loadData(){
        Api.getTransactionRecords(TYPE,mApplication.getUser().getID(),page).
                subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable throwable) {
                        completeRefresh();
                        page--;
                        if(page<PAGE_START){
                            page=PAGE_START;
                        }
                    }

                    @Override
                    public void onNext(String s) {
                        TransactionRecordDataResult result=
                                JsonUtil.json2Object(s,TransactionRecordDataResult.class);
                        boolean canLoadMore=true;
                        if(result.getCode()==Constant.SUC_CODE){
                            TransactionRecordData data=result.getData();
                            if(data!=null){
                                List<TransactionRecordData.Bean> beanList=data.getDataList();
                                if(beanList!=null&&beanList.size()>0){
                                    DebitRecordFragment.this.data.addAll(
                                            TransactionRecordData.convertShower(beanList,TYPE)
                                    );
                                    adapter.notifyDataSetChanged();
                                    canLoadMore=true;
                                }else{
                                    //无更多数据
                                    canLoadMore=false;
                                    footerLoadMore.setStateNoMoreData();
                                }
                            }
                        }else{
                            ToastUtil.showToast(result.getMsg());
                        }
                        if(srLayout.isRefreshing()){
                            srLayout.finishRefresh();
                        }
                        if(srLayout.isLoading()){
                            srLayout.finishLoadmore();
                        }
                        srLayout.setLoadmoreFinished(!canLoadMore);
                    }
                });
    }

    private void completeRefresh(){
        if(srLayout.isRefreshing()){
            srLayout.finishRefresh();
        }
        if(srLayout.isLoading()){
            srLayout.finishLoadmore();
        }
    }
}
